//
//  RHPowerGenerationCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/11.
//

#import "RHPowerGenerationCell.h"
#import "RHEfficiencyCircleView.h"
#import "RHPowerGenerationItemCell.h"


@interface RHPowerGenerationCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) RHEfficiencyCircleView *circleView;

@property (nonatomic, strong) UICollectionView *collectView;

@property (nonatomic, strong) NSArray *itemArr;
@end
@implementation RHPowerGenerationCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    //装机容量
    QMUILabel *capacityLab = [self.contentView viewWithTag:100];
    capacityLab.text = [NSString stringWithFormat:@"%ld",[dic[@"accumulatedInstalledCapacity"]integerValue]];
    
    //装机功率
    QMUILabel *powerLab = [self.contentView viewWithTag:101];
    powerLab.text = [NSString stringWithFormat:@"%ld",[dic[@"accumulatedInstalledPower"]integerValue]];
    
    NSNumber *today = [NSNumber numberWithFloat:[dic[@"todayGeneration"] floatValue]];
    NSNumber *month = [NSNumber numberWithFloat:[dic[@"monthGeneration"] floatValue]];
    NSNumber *year  = [NSNumber numberWithFloat:[dic[@"yearGeneration"] floatValue]];
    NSNumber *total = [NSNumber numberWithFloat:[dic[@"accumulatedGeneration"] floatValue]];
    
    self.itemArr = @[@{CellTitle:@"今日发电(kwh)",
                       CellContent:[today twoDecimalPlacesWithoutRounding],
                       CellIcon:@"powerGenera_01",@"color":@"FE9558"},
                      @{CellTitle:@"当月发电(kwh)",
                        CellContent:[month twoDecimalPlacesWithoutRounding],
                        CellIcon:@"powerGenera_02",@"color":@"FF649C"},
                      @{CellTitle:@"当年发电(kwh)",
                        CellContent:[year twoDecimalPlacesWithoutRounding],
                        CellIcon:@"powerGenera_03",@"color":@"31D3AE"},
                      @{CellTitle:@"累计发电(kwh)",
                        CellContent:[total twoDecimalPlacesWithoutRounding],
                        CellIcon:@"powerGenera_04",@"color":@"678EF2"}];
    
    NSString *percent = ValidNullData(dic[@"energyEfficiency"]);
    percent  = [percent stringByReplacingOccurrencesOfString:@"%" withString:@""];
    
    self.circleView.percent = [percent integerValue]/100.0;
    
    [self.collectView reloadData];
}

-(void)setupUI
{
    self.itemArr = @[@{CellTitle:@"今日发电(kwh)",CellContent:@"18.12",CellIcon:@"powerGenera_01",@"color":@"FE9558"},
                          @{CellTitle:@"当月发电(kwh)",CellContent:@"15.25",CellIcon:@"powerGenera_02",@"color":@"FF649C"},
                          @{CellTitle:@"当年发电(kwh)",CellContent:@"16.54",CellIcon:@"powerGenera_03",@"color":@"31D3AE"},
                          @{CellTitle:@"累计发电(kwh)",CellContent:@"23.45",CellIcon:@"powerGenera_04",@"color":@"678EF2"}];
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    UIView *top_bgView = kView;
    top_bgView.backgroundColor = [UIColor whiteColor];
    top_bgView.layer.cornerRadius = 8;
    top_bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:top_bgView];
    
    [top_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(0);
        make.right.mas_offset(-15);
        make.height.equalTo(top_bgView.mas_width).multipliedBy(0.5);

    }];
    
    kWeakSelf;
    _circleView = [[RHEfficiencyCircleView alloc]init];
    [top_bgView addSubview:_circleView];
    [_circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(kScaleW(30));
        make.bottom.mas_offset(kScaleW(-30));
        make.width.equalTo(weakSelf.circleView.mas_height);
    }];
    
    //累计装机容量
    QMUILabel *content1 = [[QMUILabel alloc]init];
    content1.tag = 100;
    content1.text = @"85,796";
    content1.font = [UIFont boldSystemFontOfSize:17];
    content1.textAlignment = NSTextAlignmentLeft;
    content1.textColor = kCOLOR_HEX(@"3F4443");
    [top_bgView addSubview:content1];
    
    QMUILabel *title1 = [[QMUILabel alloc]init];
    title1.text = @"累计装机容量";
    title1.font = [UIFont systemFontOfSize:15];
    title1.textAlignment = NSTextAlignmentLeft;
    title1.textColor = kCOLOR_HEX(@"3F4443");
    [top_bgView addSubview:title1];
    
    //累计装机功率
    QMUILabel *content2 = [[QMUILabel alloc]init];
    content2.tag = 101;
    content2.text = @"85,796";
    content2.font = [UIFont boldSystemFontOfSize:17];
    content2.textAlignment = NSTextAlignmentLeft;
    content2.textColor = kCOLOR_HEX(@"3F4443");
    [top_bgView addSubview:content2];
    
    QMUILabel *title2 = [[QMUILabel alloc]init];
    title2.text = @"累计装机功率";
    title2.font = [UIFont systemFontOfSize:15];
    title2.textAlignment = NSTextAlignmentLeft;
    title2.textColor = kCOLOR_HEX(@"3F4443");
    [top_bgView addSubview:title2];
    
    [content1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.circleView.mas_right).offset(kScaleW(45));
        make.top.equalTo(weakSelf.circleView).offset(8);
        make.right.mas_offset(-15);
    }];
    
    [title1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(content1);
        make.top.equalTo(content1.mas_bottom).offset(5);
    }];
    
    [title2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(content1);
        make.bottom.equalTo(weakSelf.circleView).offset(-8);
    }];
    
    [content2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title2);
        make.bottom.equalTo(title2.mas_top).offset(-5);
    }];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = CFTabViewBgColor;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHPowerGenerationItemCell class] forCellWithReuseIdentifier:@"RHPowerGenerationItemCell"];
    [self.contentView addSubview:_collectView];
    
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(top_bgView.mas_bottom).offset(15);
        make.height.offset(kScaleW(70)*2+15);
        make.bottom.mas_offset(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHPowerGenerationItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHPowerGenerationItemCell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.itemArr[indexPath.item][CellIcon]];
    cell.titleLab.text = self.itemArr[indexPath.item][CellTitle];
    cell.contentLab.text = self.itemArr[indexPath.item][CellContent];
    cell.bgView.backgroundColor = [UIColor colorWithHexString:self.itemArr[indexPath.item][@"color"] alpha:0.1];
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kUI_WINDOW_W -15*3)/2, kScaleW(70));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
@end
